package com.webank.wedatasphere.qualitis.controller.thymeleaf;

import com.webank.wedatasphere.qualitis.project.dao.repository.ProjectMuLuRepository;
import com.webank.wedatasphere.qualitis.project.dao.repository.ProjectRepository;
import com.webank.wedatasphere.qualitis.project.entity.ProjectMuLu;
import com.webank.wedatasphere.qualitis.project.entity.ProjectMuLuVo;
import com.webank.wedatasphere.qualitis.response.GeneralResponse;
import com.webank.wedatasphere.qualitis.response.entity.IndexResponse;
import com.webank.wedatasphere.qualitis.service.IndexStatisticsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class IndexController {

    @Autowired
    private ProjectRepository projectRepository;

    @Autowired
    private ProjectMuLuRepository projectMuLuRepository;

    @Resource
    private IndexStatisticsService indexStatisticsService;



    @RequestMapping(value = {"/layuiAdmin"}, method = RequestMethod.GET)
    public ModelAndView layuiAdmin() {
        ModelAndView modelAndView = new ModelAndView("layui-admin/index");


        return modelAndView;
    }

    @RequestMapping(value = {"/echarts"}, method = RequestMethod.GET)
    public ModelAndView echarts() {
        return new ModelAndView("EhcartsTable");
    }

    @RequestMapping(value = {"/index"}, method = RequestMethod.GET)
    public ModelAndView index() {
        return new ModelAndView("index");
    }



    @RequestMapping(value = {"/sjbz/muluDetail"}, method = RequestMethod.GET)
    public ModelAndView muluDetail(HttpServletRequest httpServletRequest) {
        ModelAndView view = new ModelAndView("layui-admin/sjbz/muluDetail");
        String id = httpServletRequest.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            ProjectMuLu dataInfo = projectMuLuRepository.getOne(Long.parseLong(id));
            view.addObject("dataInfo", dataInfo);
        } else {
            view.addObject("dataInfo", new ProjectMuLu());
        }
        return view;
    }

    /**
     * 数据标准管理
     *
     * @return
     */
    @RequestMapping(value = {"/sjbzGuanLi"}, method = RequestMethod.GET)
    public ModelAndView sjbzGuanLi(HttpServletRequest httpServletRequest) {
        ModelAndView view = new ModelAndView("layui-admin/sjbz/sjbzGuanLi");
        return view;
    }

    /**
     * 数据标准管理---测试台账
     *
     * @return
     */
    @RequestMapping(value = {"/sjbzGuanLiIndex"}, method = RequestMethod.GET)
    public ModelAndView sjbzGuanLiIndex() {
        ModelAndView modelAndView = new ModelAndView("layui-admin/sjbz/sjbzGuanLiIndex");
        return BuildBiaoZunManager(modelAndView);
    }

    private ModelAndView BuildBiaoZunManager(ModelAndView modelAndView) {
        List<ProjectMuLu> allList = projectMuLuRepository.findAll();
        List<ProjectMuLuVo> allListVo = new ArrayList<>();
        for (ProjectMuLu projectMuLu : allList) {
            ProjectMuLuVo projectMuLuVo = new ProjectMuLuVo();
            projectMuLuVo.setId(projectMuLu.getId());
            projectMuLuVo.setName(projectMuLu.getName());
            projectMuLuVo.setTitle(projectMuLu.getTitle());
            projectMuLuVo.setParentId(projectMuLu.getParentId());
            projectMuLuVo.setSpread(projectMuLu.getSpread().equals("true"));
            allListVo.add(projectMuLuVo);

        }
        List<ProjectMuLuVo> treeList = allListVo.stream().filter(parentNode -> parentNode.getParentId() == 0L)
                .map(node -> getChild(node, allListVo)).collect(Collectors.toList());
        modelAndView.addObject("muluData", treeList);
        return modelAndView;
    }

    /**
     * 数据标准管理----正式台账
     *
     * @return
     */
    @RequestMapping(value = {"/formalList"}, method = RequestMethod.GET)
    public ModelAndView formalList() {
        ModelAndView modelAndView = new ModelAndView("layui-admin/sjbz/formalList");
        return BuildBiaoZunManager(modelAndView);
    }

    /**
     * 递归辅助函数
     *
     * @param sql     上级节点
     * @param allList 所有数据
     */
    public ProjectMuLuVo getChild(ProjectMuLuVo sql, List<ProjectMuLuVo> allList) {
        /**
         * 1、filter：获取子节点pid是否等于id
         * 2、map：递归获取子节点
         * */
        List<ProjectMuLuVo> treeSQL = allList.stream()
                .filter(subNode -> subNode.getParentId().equals(sql.getId()))
                .map(subNode -> getChild(subNode, allList)).collect(Collectors.toList());
        sql.setChildren(treeSQL);
        return sql;
    }


    @RequestMapping(value = {"/indexCount"}, method = RequestMethod.GET)
    @ResponseBody
    public GeneralResponse<?> getCountIndex() {
        IndexResponse indexResponse = indexStatisticsService.count();
        return GeneralResponse.Ok(indexResponse);
    }
}
